#include "hm_gridview.h"
#include "mydatacenter.h"
#include <EIKENV.H>

#include "mk4.h"
#include "mk4str.h"
#include "myutils.h"

/*
 * 用来控制list View的类
 * 在list模板中，一定要设置style, width和height
 * 例如
 * <div id="dynlist"  style="width:100%;height:100%">booklist</div>
 * 否则就出现内存已满等莫名其妙的错误 2009.09.20
 */
 
extern CMyDataCenter* iDataCenter;
extern c4_Storage* gDBStore;

ChmGridView::ChmGridView()
{
	iSelected = -1;
	iControl = NULL;
}

ChmGridView::~ChmGridView()
{
	ClearData();
}


void ChmGridView::ClearData()
{

}

void ChmGridView::ParsePage(CHtmlControl* aControl)
	{
		_LIT(KIdList, "dynlist"); 
		iControl = aControl;
		CHtmlElement* item = iControl->Element(KIdList);
		if(item == NULL) return;
		
		//
		int bufsize;
		item->GetProperty(KHStrBufSize, bufsize);
		if(bufsize > 50) return;
		
		
		TBuf<50> buf;
		item->GetProperty(KHStrInnerText, buf);
		if(buf == _L("maingrid"))
			LoadMainGrid();
		if(buf == _L("contact"))
			LoadContact();
	}

//调邮件联系人数据
void ChmGridView::LoadContact()
{
{
//创建列表 
//	_LIT(KHtml, "<div id='dynlist' class='list_bg' style='width:100%;height:100%;overflow:scroll'></div>"); 
//	gHtmlControl->AppendContentL(KHtml); 
_LIT(KIdList, "dynlist"); 
CHtmlElement* iList = iControl->Element(KIdList, 0); 
iList->InvokeL(KHStrCreateList, (MListItemRenderer*)this); 

//	_LIT(KIdList, "list"); 
//	_LIT(KIdList2, "dynlist");	
//	CHtmlElement* iList = gHtmlControl->Element(KIdList);
//	if(iList == NULL)
//		iList = gHtmlControl->Element(KIdList2);
//
//	if(iList == NULL)return;	
//	
//	iList->InvokeL(KHStrCreateList, (MListItemRenderer*)this); 

//设置列表项模板 
_LIT(KListItemHtml,  
  "<div id='mail_listitem' style='width:100%; height:42;' class='list_item' focus-class='list_focus-item'>" 
"nnn"
  "</div>");

iList->SetProperty(KHStrListItemHtml, KListItemHtml);   
 
//设置当列表为空时显示的内容 
_LIT(KEmptyHtml, "<p align='center'>(No Mail)</p>"); 
iList->SetProperty(KHStrListEmptyHtml, KEmptyHtml); 
 
//设置列表项目数量 
iList->SetProperty(KHStrListItemCount, 2); 
	
//更新并重画控件 
iControl->RefreshAndDraw();	
return;
}
	//CEikonEnv::Static()->InfoWinL(_L("test"),_L("contact"));
	_LIT(KIdList, "dynlist"); 
	CHtmlElement* iList = iControl->Element(KIdList); 
	if(iList == NULL) return;
	
	iList->InvokeL(KHStrCreateList, (MListItemRenderer*)this); 
	
	c4_View view = iDataCenter->iDBMail->View("contact");
	int size = view.GetSize();
	
	//设置列表项模板 
	_LIT(KListItemHtml,  
	  "<div id='contact_item' style='width:100%; height:25; border:0' focus-class='listfocus' >"
			"<div id='icon' style='width:100%; height:100%'></div>"
			"<div id='link'>ccc</div>"			
	  "</div>");

	iList->SetProperty(KHStrListItemHtml, KListItemHtml);
	TBuf<10> tt; tt.Num(size);
	CEikonEnv::Static()->InfoMsg(tt);
	
	//设置列表项目数量 
	iList->SetProperty(KHStrListItemCount, size); 
		
	//更新并重画控件 
	iControl->RefreshAndDraw();		
}
/*
 * 从数据库里拼装main grid界面。不用customlist方式，是因为
 * 不能很好控制
 */
void ChmGridView::LoadMainGrid()
{
	_LIT(KIdList, "dynlist"); 
	CHtmlElement* iList = iControl->Element(KIdList); 
	if(iList == NULL) return;
	
	c4_View view = gDBStore->View("plugin");
	int size = view.GetSize();
	
	//设置列表项模板 
	_LIT(KListItemHtml,  
	  "<div id='grid_item' focusing='true' style='font-size:14; width:60; height:60; border:0' >"
			"<div id='icon' style='width:100%; height:100%'></div>"
			"<div id='link'>ccc</div>"			
	  "</div>");

	iList->SetProperty(KHStrInnerHtml, KNullDesC);
	for(int i = 0; i < size; i++)
		iControl->InsertContentL(iList, EBeforeEnd, KListItemHtml);
	//iList->SetProperty(KHStrInnerHtml, KListItemHtml);
	
	CHtmlElement* item = iList->Element(_L("grid_item"), 0);
	iControl->SetFocusTo(item);
	
	
	for(int i = 0; i < size; i++){
		CHtmlElement* item = iList->Element(_L("icon"), i);
		
		//item->SetProperty(KHStrInnerHtml, _L("kk"));
		TBuf<100> test;
		const TPtrC8 temp8 = DBUtil::Static()->FieldStr8Value(&view, "normal_img", i);
		TBuf<100> temp;	temp.Copy(temp8);
		test.Format(_L("background-image:mem.%S"), &temp);
		
		item->SetProperty(KHStrStyle, test);
		
		temp.Copy(DBUtil::Static()->FieldStr8Value(&view, "active_img", i));
		test.Format(_L("background-image:mem.%S"), &temp);
		item->SetProperty(KHStrFocusStyle, test);
		
		item = iList->Element(_L("link"), i);
		temp.Copy(DBUtil::Static()->FieldStr8Value(&view, "link", i));
		item->SetProperty(KHStrInnerText, temp);
	}

	 
	//更新并重画控件 
	iControl->RefreshAndDraw();		

}

void ChmGridView::ShowTabsChange()
{
}

bool ChmGridView::ProcessTabChange(bool bNextPrev)
{
	return true;
}

void ChmGridView::RenderListItemL(CHtmlElement* aElement, TInt aIndex)
{
	CHtmlElement* icon = aElement->Element(_L("icon"));
	c4_View view = iDataCenter->iDBMail->View("contact");
	TBuf<32> path;
	if(icon){
			TBuf<200> test;
			test.Copy(DBUtil::Static()->FieldStr8Value(&view, "mail", aIndex));
			
			//test.Format(_L("background-image:mem.%S"), &(data->normal_img));
			icon->SetProperty(KHStrInnerText, test);
			//test.Format(_L("background-image:mem.%S"), &(data->active_img));
			//icon->SetProperty(KHStrFocusStyle, test);//_L("background-image:mem.tab.gif;background-position:stretch"));			
	}
}

void ChmGridView::HandleHtmlCtlEventL(const THtmlCtlEvent& aEvent)
	{
	//method 1,不是虚拟表
	TBuf<200> href;
	CHtmlElement* link = aEvent.iElement->Element(_L("link"));
	if(link){

		link->GetProperty(KHStrInnerText, href);
		if(href != KNullDesC)
			iDataCenter->ProcessLink(href);
	}
	
	/*method 2
	_LIT(KIdList, "list");	
	CHtmlElement* iList = iControl->Element(KIdList);
	TInt itemIndex;
	iList->GetProperty(KHStrListSelectedIndex, itemIndex);
	//iSelected = itemIndex;	
	//iList->InvokeL(KHStrUpdate, itemIndex); 
	//CHtmlElement* title = aEvent.iElement->Element(_L("title"));
	 */

	}
